import Vue from 'vue'
import VueRouter from 'vue-router'
import HomePage from "@/views/HomePage";
import MainPage from "@/views/MainPage";
import GeneratePage from "@/components/GeneratePage";
import WordsCenter from "@/components/WordsCenter";
import MyWords from "@/components/MyWords";
import UserCenter from "@/components/UserCenter";
import {GET} from "@/util/HttpUtil";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomePage
  },
  {
    path: '/GenerationPlatform',
    name: 'main',
    component: MainPage,
    children:[
      {
        path: '/',
        redirect: '/GenerationPlatform/generate'
      },
      {
        path: 'generate',
        component: GeneratePage,
      },
      {
        path: 'wordsCenter',
        component: WordsCenter,
      },
      {
        path: 'myWords',
        component: MyWords,
      },
      {
        path: 'home',
        component: UserCenter,
      }
    ]
  }
]


const router = new VueRouter({
  mode: 'history',
  base: '/sdgp/',
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.fullPath == "/GenerationPlatform/myWords" || to.fullPath == "/GenerationPlatform/home"){
    GET("/api/common/isLogin", (res) => {
      if (res.code == 200) {
        next();
      } else{
        router.replace("/");
        sessionStorage["isLogin"] = 0;
      }
    });
  }else {
    next();
  }
});

export default router
